<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随便逛逛布局</title>
    <!-- 引入axios -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: #333;
            color: #fff;
            padding: 10px 20px;
        }
        header nav a {
            color: #fff;
            text-decoration: none;
            margin: 0 10px;
        }
        .search-bar {
            flex-grow: 1;
            margin: 0 20px;
        }
        .search-bar input {
            width: 100%;
            padding: 5px;
        }
        .carousel {
            background-color: #ddd;
            height: 200px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 20px 0;
            font-size: 24px;
            color: #555;
        }
        .tabs {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
        }
        .tabs .categories, .tabs .filters {
            display: flex;
            gap: 10px;
        }
        .tabs button {
            padding: 10px 20px;
            border: none;
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }
        .tabs button:hover {
            background-color: #0056b3;
        }
        img{
            width: 250px;
            height: 250px;
        }
        .gallery {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }
        .gallery .item {
            background-color: #fff;
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
            position: relative;
        }
        .gallery .item .actions {
            display: flex;
            justify-content: space-around;
            margin-top: 10px;
        }
        .gallery .item .actions button {
            padding: 5px 10px;
            border: none;
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }
        .gallery .item .actions button:hover {
            background-color: #0056b3;
        }
        .recommendations {
            margin: 20px 0;
        }
        .recommendations h2 {
            margin-bottom: 10px;
        }
        .recommendations .content {
            display: flex;
            gap: 20px;
        }
        .recommendations .content div {
            flex: 1;
            background-color: #fff;
            padding: 20px;
            text-align: center;
        }
        footer {
            background-color: #333;
            color: #fff;
            text-align: center;
            padding: 20px;
            margin-top: 20px;
        }
        .modal {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            z-index: 1000;
            width: 400px;
            text-align: center;
        }
        .modal.active {
            display: block;
        }
        .overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 999;
        }
        .overlay.active {
            display: block;
        }
        .payment-methods {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin: 20px 0;
        }
        .payment-methods img {
            width: 50px;
            height: 50px;
            cursor: pointer;
            border: 2px solid transparent;
        }
        .payment-methods img.selected {
            border-color: #007bff;
        }
        .modal button {
            padding: 10px 20px;
            margin: 10px;
            border: none;
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }
        .modal button:hover {
            background-color: #0056b3;
        }
        .filter-buttons {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        .filter-buttons button {
            padding: 10px 20px;
            border: none;
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }
        .filter-buttons button.active {
            background-color: #0056b3;
        }
        /*vip样式  */
        .vip-package {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px;
            border: 2px solid #ddd;
            border-radius: 8px;
            background-color: #f9f9f9;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .vip-package:hover {
            border-color: #007bff;
            background-color: #e6f3ff;
        }
        
        .vip-package.selected {
            border-color: #007bff;
            background-color: #007bff;
            color: #fff;
        }
        
        .vip-package.selected .price,
        .vip-package.selected .discount {
            color: #fff;
        }
        
        .vip-package .details {
            flex: 1;
        }
        
        .vip-package .name {
            font-size: 1.1em;
            font-weight: bold;
        }
        
        .vip-package .price {
            font-size: 1em;
            color: #333;
        }
        
        .vip-package .discount {
            font-size: 0.9em;
            color: #28a745;
        }
        
        .vip-package .duration {
            font-size: 0.9em;
            color: #666;
        }
        
        .vip-package.selected .duration {
            color: #fff;
        }
        .vip-package.selected::after {
            content: '✔️';
            margin-left: 10px;
            font-size: 1.2em;
        }
        .vip-packages select {
            width: 100%;
            padding: 10px;
            border: 2px solid #ddd;
            border-radius: 8px;
            background-color: #fff;
            font-size: 1em;
            color: #333;
            cursor: pointer;
            transition: border-color 0.3s ease;
        }
        
        .vip-packages select:hover {
            border-color: #007bff;
        }
        
        .vip-packages select:focus {
            border-color: #007bff;
            outline: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 头部 -->
        <header>
            <nav>
                <a href="./user.html">个人页面</a>
                <a href="./productivitydemo.html">收藏夹</a>
                <a href="#">关于我们</a>
            </nav>
            <div class="search-bar">
                <input type="text" placeholder="搜索作品或艺术家...">
            </div>
            <div>
                <a href="./login.html">登录</a> | 
                <a href="./login.html">注册</a> | 
                <button onclick="showModal()">充值付费</button>
            </div>
        </header>

        <!-- 轮播图 -->
        <div class="carousel">
            轮播图/推荐作品
        </div>

        <!-- 推荐内容 -->
        <div class="recommendations">
            <h2>热门推荐</h2>
            <div class="content">
                <div>热门作品</div>
                <div>VIP专区</div>
                <div>最新上传</div>
            </div>
        </div>

         <!-- 标签栏 -->
         <div class="tabs">
            <div class="categories">
                <input type="checkbox" value="1" name="style[]"/>风景
                <input type="checkbox" value="2" name="style[]"/>人物
                <input type="checkbox" value="3" name="style[]"/>抽象
                <input type="checkbox" value="4" name="style[]"/>二次元
                <input type="checkbox" value="4" name="style[]"/>其他
                <button>筛选</button>
            </div>
            <div class="filters">
                <select id="sort-select">
                    <option value="default">默认排序</option>
                    <option value="becollected">收藏最多</option>
                    <option value="beliked">最受欢迎</option>
                </select>
            </div>
        </div>

        <!-- 筛选按钮 -->
        <div class="filter-buttons">
            <button class="active" onclick="filterImages('all')">全部</button>
            <button onclick="filterImages('free')">免费</button>
            <button onclick="filterImages('vip')">VIP</button>
        </div>

        <!-- 作品展示 -->
        <div class="gallery" id="gallery">
            <!-- 图片将通过JavaScript动态加载 -->
        </div>

        <!-- 页脚 -->
        <footer>
            <p>关于我们 | 联系方式 | 隐私政策 | 服务条款</p>
            <p>© 2025 MJ SEED</p>
        </footer>

        <!-- 弹窗 -->
        <div class="overlay" id="overlay"></div>
        <div class="modal" id="modal">
            <h2>选择支付方式</h2>
            <!-- VIP 套餐选择区域 -->
            <div class="vip-packages">
                <h3>VIP 套餐</h3>
                <select id="vip-packages-list">
                    <!-- VIP 套餐将通过 JavaScript 动态加载 -->
                </select>
            </div>
            <div class="payment-methods">
                <h3>选择支付方式</h3>
                <img src="./wx.png" alt="微信支付" onclick="selectPayment('wechat')" id="wechat">
                <img src="./zfb.jpg" alt="支付宝支付" onclick="selectPayment('alipay')" id="alipay">
            </div>
            <div id="qr-code">
                <!-- 二维码图片将根据选择的支付方式动态加载 -->
                <img src="./2420.jpg_wh860.jpg" alt="微信支付二维码" id="qr-image">
            </div>
            <button onclick="confirmPayment()">确认支付</button>
            <button onclick="hideModal()">关闭</button>
        </div>

        <script>
            // 显示弹窗
            function showModal() {
                document.getElementById('modal').classList.add('active');
                document.getElementById('overlay').classList.add('active');
            }
    
            // 隐藏弹窗
            function hideModal() {
                document.getElementById('modal').classList.remove('active');
                document.getElementById('overlay').classList.remove('active');
            }
    
            // 选择支付方式
            function selectPayment(method) {
                const wechat = document.getElementById('wechat');
                const alipay = document.getElementById('alipay');
                const qrImage = document.getElementById('qr-image');
    
                if (method === 'wechat') {
                    wechat.classList.add('selected');
                    alipay.classList.remove('selected');
                    qrImage.src = './wx.png';
                } else if (method === 'alipay') {
                    alipay.classList.add('selected');
                    wechat.classList.remove('selected');
                    qrImage.src = './zfb.jpg';
                }
            }
    
            // 获取选中的套餐类型
            function getSelectedPackageType() {
                const packagesList = document.getElementById('vip-packages-list');
                const selectedOption = packagesList.options[packagesList.selectedIndex];
                if (selectedOption.value === '') {
                    return null; // 如果没有选择套餐，返回 null
                }
                // 从选项的文本内容中提取套餐类型
                const packageTypeText = selectedOption.textContent;
                if (packageTypeText.includes('年费套餐')) {
                    return 'annual';
                } else if (packageTypeText.includes('月费套餐')) {
                    return 'monthly';
                } else if (packageTypeText.includes('日费套餐')) {
                    return 'daily';
                }
                return null;
            }
    
            // 确认支付
            async function confirmPayment() {
                const selectedPayment = document.querySelector('.payment-methods img.selected');
                if (!selectedPayment) {
                    alert('请选择支付方式！');
                    return;
                }
    
                const selectedPackageType = getSelectedPackageType();
                if (!selectedPackageType) {
                    alert('请选择 VIP 套餐！');
                    return;
                }
    
                // 静态值
                const user_id = 6;
                const payment_id = "PAY_MONTH_123";
                const use_discount = true; // 假设使用折扣
    
                // 构造请求体
                const requestBody = {
                    user_id: user_id,
                    payment_id: payment_id,
                    package_type: selectedPackageType, // 使用用户选择的套餐类型
                    use_discount: use_discount
                };
    
                try {
                    const response = await axios.post('http://127.0.0.1:5000/vip/renew', requestBody, {
                        headers: {
                            'Content-Type': 'application/json'
                        }
                    });
    
                    if (response.status === 200) {
                        const result = response.data;
                        alert(`续费成功！\n金额: ${result.amount_charged}\n有效期至: ${result.end_date}`);
                        hideModal();
                    } else {
                        alert(`续费失败: ${response.data.message}`);
                    }
                } catch (error) {
                    console.error('Error:', error);
                    if (error.response) {
                        alert(`支付失败: ${error.response.data.message}`);
                    } else {
                        alert('支付失败，请稍后重试！');
                    }
                }
            }
    
            // 获取图片数据
            async function fetchImages(sortBy = 'default') {
                try {
                    const response = await fetch(`http://127.0.0.1:5000/admin/images?sort_by=${sortBy}`);
                    const data = await response.json();
                    console.log('Images:', data);
                    // 根据 sortBy 参数对图片进行排序
                    if (sortBy === 'becollected') {
                        data.sort((a, b) => b.becollected - a.becollected); // 按收藏数从高到低排序
                    } else if (sortBy === 'beliked') {
                        data.sort((a, b) => b.beliked - a.beliked); // 按喜欢数从高到低排序
                    }
    
                    return data;
                } catch (error) {
                    console.error('Error fetching images:', error);
                }
            }
    
            // 渲染图片列表
            function renderImages(images) {
                const gallery = document.getElementById('gallery');
                gallery.innerHTML = '';
                images.forEach(image => {
                    const item = document.createElement('div');
                    item.className = 'item';
                    item.innerHTML = `
                        <a href="./detailspage.html?image_id=${image.image_id}">
                            <img src="${image.oss_path}" alt="${image.image_title}">
                        </a>
                        <h3 style="margin: 10px 0 5px; font-size: 1.1em;">${image.image_title}</h3>
                        <p style="margin: 0; color: #666; font-size: 0.9em;">${image.image_tag1}, ${image.image_tag2}, ${image.image_tag3}</p>
                        <p style="margin: 5px 0; color: #666; font-size: 0.9em;">收藏数: ${image.becollected}</p>
                        <p style="margin: 5px 0; color: #666; font-size: 0.9em;">点赞数: ${image.beliked}</p>
                        <div class="actions">
                            <button onclick="copyImage()">复制</button>
                            <button onclick="downloadImage()">下载</button>
                            <button onclick="alert('收藏成功！');">收藏</button>
                        </div>
                    `;
                    gallery.appendChild(item);
                });
            }
    
            // 筛选图片
            function filterImages(filter, sortBy = 'default') {
                const buttons = document.querySelectorAll('.filter-buttons button');
                buttons.forEach(button => button.classList.remove('active'));
                document.querySelector(`.filter-buttons button[onclick="filterImages('${filter}')"]`).classList.add('active');
    
                fetchImages(sortBy).then(images => {
                    if (filter === 'all') {
                        renderImages(images);
                    } else if (filter === 'free') {
                        const freeImages = images.filter(image => image.vip_exclusive === 'no');
                        renderImages(freeImages);
                    } else if (filter === 'vip') {
                        const vipImages = images.filter(image => image.vip_exclusive === 'yes');
                        renderImages(vipImages);
                    }
                });
            }
    
            // 初始化页面
            document.addEventListener('DOMContentLoaded', () => {
                filterImages('all', 'default');
            });
    
            // 排序功能
            document.querySelector('.tabs select').addEventListener('change', function(event) {
                const sortBy = event.target.value;
                console.log(`Sort by ${sortBy}`);
                filterImages('all', sortBy); // 传递排序参数
            });
    
            // 获取 VIP 套餐数据
            async function fetchVipPackages() {
                try {
                    const response = await axios.get('http://127.0.0.1:5000/admin/vip-packages');
                    return response.data;
                } catch (error) {
                    console.log('Error fetching VIP packages:', error);
                    return [];
                }
            }
    
            // 渲染 VIP 套餐列表
            function renderVipPackages(packages) {
                const packagesList = document.getElementById('vip-packages-list');
                packagesList.innerHTML = '';
    
                // 添加默认选项
                const defaultOption = document.createElement('option');
                defaultOption.value = '';
                defaultOption.textContent = '请选择 VIP 套餐';
                defaultOption.disabled = true;
                defaultOption.selected = true;
                packagesList.appendChild(defaultOption);
    
                // 添加套餐选项
                packages.forEach(pkg => {
                    const option = document.createElement('option');
                    option.value = pkg.package_id;
                    option.textContent = `${pkg.package_type === 'annual' ? '年费套餐' : pkg.package_type === "monthly" ? '月费套餐' : '日费套餐'} - ¥${pkg.current_price.toFixed(2)} (${pkg.duration_days} 天)`;
                    packagesList.appendChild(option);
                });
            }
    
            // 获取选中的套餐 ID
            function getSelectedPackageId() {
                const packagesList = document.getElementById('vip-packages-list');
                return packagesList.value;
            }
    
            // 显示弹窗时加载 VIP 套餐
            function showModal() {
                document.getElementById('modal').classList.add('active');
                document.getElementById('overlay').classList.add('active');
    
                // 加载 VIP 套餐
                fetchVipPackages().then(packages => {
                    renderVipPackages(packages);
                });
            }
        </script>
</body>
</html>